
package com.jcbase.controller.sys;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.jcbase.core.auth.anno.RequiresPermissions;
import com.jcbase.core.controller.JCBaseController;
import com.jcbase.core.model.Condition;
import com.jcbase.core.model.Operators;
import com.jcbase.core.util.CommonUtils;
import com.jcbase.core.util.IWebUtils;
import com.jcbase.core.view.InvokeResult;
import com.jcbase.model.SysObj;
import com.jcbase.model.SysObjGroup;
import com.jcbase.model.SysRole;
import com.jcbase.model.SysUser;
import com.jcbase.model.SysUserRole;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 资源组管理
 * 
 * @author Administrator
 *
 */

public class ObjGroupController extends JCBaseController {

    @RequiresPermissions(value = { "/sys/objGroup" })
    public void index() {
	InvokeResult result = SysObjGroup.me.getZtreeViewList(getRequest());
	this.setAttr("jsonTree", result);
	render("obj_index.jsp");
    }

    public void refreshTree() {
	InvokeResult result = SysObjGroup.me.getZtreeViewList(getRequest());
	this.setAttr("jsonTree", result);
	this.renderJson(result);
    }

    public void getAllTree() {
	Integer roleId = this.getParaToInt("roleId");
	InvokeResult result = SysObjGroup.me.getZtreeViewList(roleId);

	this.setAttr("jsonTree", result);
	this.setAttr("roleId", roleId);
	render("role_obj_setting.jsp");
    }

    @RequiresPermissions(value = { "/sys/objGroup" })
    public void add() {
	Integer pid = this.getParaToInt("pid");
	Integer gid = this.getParaToInt("gid");
	if (gid != null) {
	    this.setAttr("item", SysObjGroup.me.findById(gid));
	}
	this.setAttr("gid", gid);
	this.setAttr("pid", pid);
	render("obj_group_add.jsp");
    }

    @RequiresPermissions(value = { "/sys/objGroup" })
    public void getListData() {
	String name = this.getPara("name");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	Set<Condition> conditions = new HashSet<Condition>();
	if (CommonUtils.isNotEmpty(name)) {
	    conditions.add(new Condition("name", Operators.LIKE, name));
	}
	List<SysUserRole> list = SysRole.me.getRole(getRequest());
	Page<SysObj> pageInfo;
	if (SysRole.me.checkIsAdmin(list)) {
	    pageInfo = SysObj.me.getPage(getPage(), this.getRows(), conditions);
	} else {
	    pageInfo = SysObj.me.getPage(this.getPage(), this.getRows(), "select * ",
		    "from sys_obj where id in (select obj_id from sys_role_obj where role_id = (select role_id from sys_user_role where user_id = "
			    + user.getId() + "))",
		    this.getOrderby());
	}
	this.renderJson(SysObj.me.toJqGridView(pageInfo));
    }

    /**
     * 获取改组下所有用户
     */
    @RequiresPermissions(value = { "/sys/objGroup" })
    public void getGroupListData() {
	String id = this.getPara("id");
	List<SysObjGroup> groups = SysObjGroup.me.find("select * from sys_obj_group where p_id = " + id);
	String str = "(" + id + ",";
	for (SysObjGroup sysGroup : groups) {
	    str = str + sysGroup.getId() + ",";
	}
	str = str.substring(0, str.length() - 1) + ")";
	Page<SysObj> pageInfo = SysObj.me.getPage(this.getPage(), this.getRows(), "select * ",
		"from sys_obj where group_id in " + str, this.getOrderby());
	this.renderJson(SysObj.me.toJqGridView(pageInfo));
    }

    @RequiresPermissions(value = { "/sys/objGroup" })
    public void delete() {
	String id = this.getPara("id");
	String type = this.getPara("type");
	if ("group".equals(type)) {
	    Db.update("update sys_obj_group set p_id = null where p_id = " + id);
	    Db.update("update sys_obj set group_id = null where group_id = " + id);
	    InvokeResult result = SysObjGroup.me.delete(id);
	    this.renderJson(result);
	}
	if ("user".equals(type)) {
	    Db.update("update sys_obj set group_id = null where id = " + id);
	    this.renderJson(InvokeResult.success());
	}
    }

    @RequiresPermissions(value = { "/sys/objGroup" })
    public void getZtree() {
	Integer gid = this.getParaToInt("gid");
	InvokeResult result = SysObj.me.getZtreeViewList();
	this.setAttr("jsonTree", result);
	this.setAttr("groupId", gid);
	render("group_obj_setting.jsp");
    }

    @RequiresPermissions(value = { "/sys/objGroup" })
    @Before(Tx.class)
    public void saveUserGroup() {
	Integer groupId = this.getParaToInt("groupId");
	String objIds = this.getPara("ObjIds");
	InvokeResult result = SysObjGroup.me.updateUserGroup(objIds, groupId);
	this.renderJson(result);
    }

    @RequiresPermissions(value = { "/sys/objGroup" })
    public void deleteGroup() {
	String objs = this.getPara("objs");
	InvokeResult result = SysObjGroup.me.deleteUserGroup(objs);
	this.renderJson(result);
    }

    @RequiresPermissions(value = { "/sys/objGroup" })
    public void save() {
	Integer pid = this.getParaToInt("pid");
	Integer gid = this.getParaToInt("gid");
	InvokeResult result;
	// 添加分组
	if (gid == null) {
	    result = SysObjGroup.me.addSave(pid, this.getPara("name"));
	} else {
	    result = SysObjGroup.me.updateSave(gid, this.getPara("name"));
	}
	this.renderJson(result);
    }

}
